//-----------------------------------------------------------------------
//<copyright file="ChiTietPhieuNhapDAO.cs
//Code generate by tools.
//</copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using PMS.Exceptions;
using System.Data.SqlClient;
using PMS.Entity;

namespace PMS.DAL
{
	public class ChiTietPhieuNhapDAO : SQLDatabase, IChiTietPhieuNhapDAO
	{

		#region Create ChiTietPhieuNhap methods

		public ChiTietPhieuNhap CreateChiTietPhieuNhap(ChiTietPhieuNhap chiTietPhieuNhap)
		{
			try
			{

				SqlCommand command = SQLDatabase.StoredProcedureCommand("spChiTietPhieuNhapInsert");
                AddInParameter(command, "@MaCTPN", SqlDbType.NVarChar, chiTietPhieuNhap.MaCTPN);
                AddInParameter(command, "@SoPN", SqlDbType.NVarChar, chiTietPhieuNhap.SoPN);
                AddInParameter(command, "@MaLuuKho", SqlDbType.Int, chiTietPhieuNhap.MaLuuKho);
                AddInParameter(command, "@DonGia", SqlDbType.Int, chiTietPhieuNhap.DonGia);
                AddInParameter(command, "@SoLuong", SqlDbType.Int, chiTietPhieuNhap.SoLuong);

				ExecuteNonQuery(command);
				return chiTietPhieuNhap;
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access CreateChiTietPhieuNhapException", ex);
			}
		}

		#endregion

		#region Update ChiTietPhieuNhap methods

		public void UpdateChiTietPhieuNhap(ChiTietPhieuNhap chiTietPhieuNhap)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spChiTietPhieuNhapUpdate");

                AddInParameter(command, "@MaCTPN", SqlDbType.NVarChar, chiTietPhieuNhap.MaCTPN);
                AddInParameter(command, "@SoPN", SqlDbType.NVarChar, chiTietPhieuNhap.SoPN);
                AddInParameter(command, "@MaLuuKho", SqlDbType.Int, chiTietPhieuNhap.MaLuuKho);
                AddInParameter(command, "@DonGia", SqlDbType.Int, chiTietPhieuNhap.DonGia);
                AddInParameter(command, "@SoLuong", SqlDbType.Int, chiTietPhieuNhap.SoLuong);

				ExecuteNonQuery(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access UpdateChiTietPhieuNhapException", ex);
			}
		}

        public void UpdateChiTietPhieuNhapTonkho(string maCTN, int Qty)
        {
            try
            {
                SqlCommand command = SQLDatabase.StoredProcedureCommand("spChiTietPhieuNhapUpdateTonKho");

                AddInParameter(command, "@MaCTPN", SqlDbType.NVarChar, maCTN);
                AddInParameter(command, "@TonKho", SqlDbType.Int, Qty);

                ExecuteNonQuery(command);
            }
            catch (Exception ex)
            {
                throw DataAccessException.GetException("Data Access UpdateChiTietPhieuNhapException", ex);
            }
        }

		#endregion

		#region Delete ChiTietPhieuNhap methods

		public void DeleteChiTietPhieuNhap(ChiTietPhieuNhap chiTietPhieuNhap)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spChiTietPhieuNhapDelete");

				AddInParameter(command, "@MaCTPN", SqlDbType.NVarChar, chiTietPhieuNhap.MaCTPN);

				ExecuteNonQuery(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access DeleteChiTietPhieuNhapException", ex);
			}
		}

        public void DeleteChiTietPhieuNhapBySoPN(string SoPN)
        {
            try
            {
                SqlCommand command = SQLDatabase.StoredProcedureCommand("spChiTietPhieuNhapDeleteAllBySoPN");

                AddInParameter(command, "@SoPN", SqlDbType.NVarChar, SoPN);

                ExecuteNonQuery(command);
            }
            catch (Exception ex)
            {
                throw DataAccessException.GetException("Data Access DeleteChiTietPhieuNhapException", ex);
            }
        }

		#endregion

		#region Select methods

		#region GetAll ChiTietPhieuNhap methods

		public ChiTietPhieuNhaps GetAllChiTietPhieuNhap()
		{
			try
			{

				SqlCommand command = SQLDatabase.StoredProcedureCommand("spChiTietPhieuNhapGetList");
				ChiTietPhieuNhaps Result = new ChiTietPhieuNhaps();
				List<ChiTietPhieuNhap> List = ExecuteQuery<ChiTietPhieuNhap>(command,true);
				foreach(ChiTietPhieuNhap item in List)
				{
					Result.Add(item);
				}
				return Result;
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access GetAllChiTietPhieuNhapException", ex);
			}
		}

        public ChiTietPhieuNhaps GetAllChiTietPhieuNhapBySoPN(string SoPN)
        {
            try
            {

                SqlCommand command = SQLDatabase.StoredProcedureCommand("spChiTietPhieuNhapSelectAllBySoPN");
                AddInParameter(command, "@SoPN", SqlDbType.NVarChar, SoPN);
                ChiTietPhieuNhaps Result = new ChiTietPhieuNhaps();
                List<ChiTietPhieuNhap> List = ExecuteQuery<ChiTietPhieuNhap>(command, true);
                foreach (ChiTietPhieuNhap item in List)
                {
                    Result.Add(item);
                }
                return Result;
            }
            catch (Exception ex)
            {
                throw DataAccessException.GetException("Data Access GetAllChiTietPhieuNhapException", ex);
            }
        }

        public ChiTietPhieuNhaps GetAllThuocHetHangTrongThoiGian(int SoNGay)
        {
            try
            {

                SqlCommand command = SQLDatabase.StoredProcedureCommand("spDanhSachThuocHetHan");
                AddInParameter(command, "@songay", SqlDbType.NVarChar, SoNGay);
                ChiTietPhieuNhaps Result = new ChiTietPhieuNhaps();
                List<ChiTietPhieuNhap> List = ExecuteQuery<ChiTietPhieuNhap>(command, true);
                foreach (ChiTietPhieuNhap item in List)
                {
                    Result.Add(item);
                }
                return Result;
            }
            catch (Exception ex)
            {
                throw DataAccessException.GetException("Data Access GetAllChiTietPhieuNhapException", ex);
            }
        }

        public ChiTietPhieuNhaps GetAllChiTietPhieuNhapByNgayNhapAndSoPNAndMaThuoc(string SoPN, DateTime? tungay, DateTime? dengnay, string mathuoc)
        {
            try
            {

                SqlCommand command = SQLDatabase.StoredProcedureCommand("spChiTietPhieuNhapSelectAllByNgayNhapAndSoPNAndMaThuoc");
                AddInParameter(command, "@SoPN", SqlDbType.NVarChar, SoPN);
                AddInParameter(command, "@Mathuoc", SqlDbType.NVarChar, mathuoc);
                AddInParameter(command, "@tungay", SqlDbType.DateTime, tungay);
                AddInParameter(command, "@denngay", SqlDbType.DateTime, dengnay);
                ChiTietPhieuNhaps Result = new ChiTietPhieuNhaps();
                List<ChiTietPhieuNhap> List = ExecuteQuery<ChiTietPhieuNhap>(command, true);
                foreach (ChiTietPhieuNhap item in List)
                {
                    Result.Add(item);
                }
                return Result;
            }
            catch (Exception ex)
            {
                throw DataAccessException.GetException("Data Access GetAllChiTietPhieuNhapException", ex);
            }
        }

        public ChiTietPhieuNhaps GetAllChiTietPhieuNhapByMaThuoc(string mathuoc,string makho, int? lonhon)
        {
            try
            {

                SqlCommand command = SQLDatabase.StoredProcedureCommand("spChiTietPhieuNhapSelectAllByMaThuoc");
                AddInParameter(command, "@MaThuoc", SqlDbType.NVarChar, mathuoc);
                AddInParameter(command, "@MaKho", SqlDbType.NVarChar, makho);
                AddInParameter(command, "@StoreMoreThan", SqlDbType.Int, lonhon);
                ChiTietPhieuNhaps Result = new ChiTietPhieuNhaps();
                List<ChiTietPhieuNhap> List = ExecuteQuery<ChiTietPhieuNhap>(command, true);
                foreach (ChiTietPhieuNhap item in List)
                {
                    Result.Add(item);
                }
                return Result;
            }
            catch (Exception ex)
            {
                throw DataAccessException.GetException("Data Access GetAllChiTietPhieuNhapException", ex);
            }
        }
		#endregion


		#region GetChiTietPhieuNhap methods

		public ChiTietPhieuNhap GetChiTietPhieuNhapByID(string maCTPN)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spChiTietPhieuNhapGet");

				AddInParameter(command, "@maCTPN", SqlDbType.NVarChar, maCTPN);

				return ExecuteQuery<ChiTietPhieuNhap>(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access GetChiTietPhieuNhapBy ID Exception", ex);
			}
		}

        public ChiTietPhieuNhap GetChiTietPhieuNhapByMaThuocAndSoLo(string mathuoc, string solo)
        {
            try
            {
                SqlCommand command = SQLDatabase.StoredProcedureCommand("spChiTietPhieuNhapGetByMaThuocAndSoLo");

                AddInParameter(command, "@MaThuoc", SqlDbType.NVarChar, mathuoc);
                AddInParameter(command, "@SoLo", SqlDbType.NVarChar, solo);

                return ExecuteQuery<ChiTietPhieuNhap>(command);
            }
            catch (Exception ex)
            {
                throw DataAccessException.GetException("Data Access GetChiTietPhieuNhapBy ID Exception", ex);
            }
        }

		#endregion


		#endregion
	}
}//End Class
